using System.ComponentModel;
using System.Diagnostics.Metrics;
using System.Runtime.InteropServices;
using Citect;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace CtApi4Docer.Controllers
{
    [ApiController]
    [Route("system-model")]
    public class WeatherForecastController : ControllerBase
    {

       

        private static readonly string[] Summaries = new[]
        {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }

        [HttpPost(template:"equipment",Name = "GetCluster")]
        public IEnumerable<Dictionary<string, string>> GetCluster()
        {
            var propertiesName = new[] { "NAME" };
            var h = CtApi.CtOpen("172.26.172.124", "admin", "admin", 0);
            IntPtr oh = default;
            var hfindptr = IntPtr.Zero;
            var hfind = CtApi.CtFindFirstEx(h, "Equip", "", "", ref oh, 0);
            if (hfind == IntPtr.Zero)
            {
                var error = new Win32Exception(Marshal.GetLastWin32Error());
                throw error;
            }

            var items = new List<Dictionary<string, string>>();
            do
            {
                var item = new Dictionary<string, string>();
                foreach (var propertyName in propertiesName)
                {
                    item[propertyName] = CtApi.GetProperty(hfindptr, propertyName);
                }

                items.Add(item);
            } while (CtApi.CtFindNext(hfind, ref hfindptr));
            CtApi.CtFindClose(hfind);


            return items;
        }
    }
}